package org.geekbang.thinking.in.spring.bean.lifecycle;

import org.geekbang.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;

import java.util.Map;

/**
 * BeanDefinition 合并示例
 *
 * @author: wangsiqi
 * @date: 2021/10/30
 */
public class MergedBeanDefinitionDemo {

    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 基于 XML 资源的 BeanDefinitionReader 实现
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        // 加载 BeanDefinition
        EncodedResource encodedResource = new EncodedResource(new ClassPathResource("META-INF/dependency-lookup-context.xml"), "UTF-8");
        int beanCounts = beanDefinitionReader.loadBeanDefinitions(encodedResource);
        System.out.println("已加载  BeanDefinition 数量：" + beanCounts);

        // 查找 Bean
        User user = beanFactory.getBean("user", User.class);
        System.out.println("user: " + user);
        User superUser = beanFactory.getBean("superUser", User.class);
        System.out.println("superUser: " + superUser);
    }

}
